package cn.android.support.v7.lib.sin.crown.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.model.Single;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 单选框SingleChoose的集合。男，女。高度固定为了两行的高。
 * Created by 彭治铭 on 2017/9/16.
 */

public class SingleChooses extends ViewGroup implements BaseListenner<SingleChoose> {
    public SingleChooses(Context context) {
        super(context);
    }

    public SingleChooses(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //位图,常态,选中
    Bitmap general, selected;

    //设置自定义位图，需要自己对位图进行适配。内部不会再适配
    public void setBitmap(Bitmap general, Bitmap selected) {
        this.general = general;
        this.selected = selected;
        requestLayout();
    }

    //根据数据自动添加子View
    public void addSingle(Single single) {
        SingleChoose singleChoose = new SingleChoose(getContext());
        singleChoose.setSingle(single);
        addView(singleChoose);
    }

    public void addSingles(List<Single> singles) {
        for (int i = 0; i < singles.size(); i++) {
            SingleChoose singleChoose = new SingleChoose(getContext());
            singleChoose.setSingle(singles.get(i));
            addView(singleChoose);
        }
    }

    //构造函数之后，最先执行，会多次测量，执行多次【差不多3次】。测量，决定ViewGroup的大小。
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (maxWidth <= 0) {
            maxWidth = ProportionUtils.getInstance().adapterInt(104);
        }
        if (maxHeight <= 0) {
            maxHeight = ProportionUtils.getInstance().adapterInt(120);
        }
        setMeasuredDimension(maxWidth, maxHeight);
    }

    //子View之间上下间隙
    int offsetTop = ProportionUtils.getInstance().adapterInt(20);
    //单个子View的最大宽度
    int maxWidth = 0;
    int maxHeight = 0;

    //在onMeasure()测量之后执行。如果onMeasure()多次执行，onLayout()也会紧随其后，多次执行。
    //控制子View的布局。onLayout()不会循环。需要内部自己循环绘制出所有的子View
    //addView(childView);调用之后，会自动刷新布局。
    //getChildAt(0).invalidate();仅仅只会刷新对于的子View。对ViewGroup没影响,但是对子View是有影响的。
    //requestLayout();重新布局，會重新調用onMeasure(),onLayout(),dispatchDraw();即重新布局和绘画。和View一样。
    //invalidate();只会调用dispatchDraw();
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        //changed 如果ViewGruop的布局发生变化，会重新调用onLayout()。
        //left,top,right,bottom。是ViewGroup相对于父容器的位置。
        left = 0;
        top = 0;
        // 动态获取子View实例，循环一次性绘制出所有的子View
        for (int i = 0, size = getChildCount(); i < size; i++) {
            SingleChoose view = (SingleChoose) getChildAt(i);
            //view.layout()是相对于ViewGroup左上角(0,0)为标准。
            //view调用完了layout()之后，view.getWidth()和view.getHeight()都能获取到具体的值。
            view.layout(left, top, view.width, top + view.height + offsetTop);//给子View分配区间大小，此时子View的onMeasure()不会执行，将失去意义。大小由ViewGroup分配。
            top += view.getHeight() + offsetTop;
            maxWidth = view.getWidth() > maxWidth ? view.getWidth() : maxWidth;
            maxHeight = top;
            if (i == size - 1) {
                //最后一个不需要间隙
                maxHeight -= offsetTop;
            }
            if (general != null && selected != null) {
                view.setBitmap(general, selected);
            }
            if (!this.single) {
                //多选，开启取消
                view.setCancel(true);
            }

            view.setOnStateChangeListener(this);
            requestLayout();
        }
    }

    //ViewGroup只会调用dispatchDraw(),不会调用draw()。最后一次onLayout()调用完之后调用。
    //控制子View的绘图
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);//这里会调用drawChild()绘制子View。如果屏蔽则无法绘制子View
    }

    public boolean single = true;//false多选。true 单选，默认就是单选

    //设置是否单选，false多选。true 单选
    public void setSingle(boolean single) {
        this.single = single;
        if (!single) {
            //多选，开启取消
            for (int i = 0; i < getChildCount(); i++) {
                SingleChoose singleChoose = (SingleChoose) getChildAt(i);
                singleChoose.setCancel(true);
            }
        }
    }

    //获取选中的Single实体类。单选，多选都适用。
    public List<Single> getSingles() {
        List<Single> singles = new ArrayList<>();
        for (int i = 0; i < getChildCount(); i++) {
            SingleChoose singleChoose = (SingleChoose) getChildAt(i);
            if (singleChoose.isState()) {
                singles.add(singleChoose.getSingle());
            }
        }
        return singles;
    }

    //根据下标，设置选中项。
    public void selected(int index) {
        if (index < getChildCount() && index >= 0) {
            SingleChoose singleChoose = (SingleChoose) getChildAt(index);
            singleChoose.setState(true);
        }
    }

    //没有选择,全部都设置成未选中。
    public void noSelected() {
        for (int i = 0; i < getChildCount(); i++) {
            SingleChoose singleChoose = (SingleChoose) getChildAt(i);
            singleChoose.setState(false);
        }
    }

    //单选回调。选中改变时回调
    BaseListenner<List<Single>> baseListenner;

    public void setBaseListenner(BaseListenner<List<Single>> baseListenner) {
        this.baseListenner = baseListenner;
    }

    @Override
    public void onResult(SingleChoose singleChoose) {
        if (single && singleChoose.isState()) {
            for (int i = 0; i < getChildCount(); i++) {
                SingleChoose singleChoose1 = (SingleChoose) getChildAt(i);
                if (singleChoose1.isState() && singleChoose1 != singleChoose) {
                    singleChoose1.setState(false);
                }
            }
        }
        if (baseListenner != null && singleChoose.isState()) {
            baseListenner.onResult(getSingles());
        }
    }
}
